﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BYRBBS.Model;
using BYRBBS.Util;
using BYRBBS.MyDebug;
using System.Diagnostics;
using Microsoft.Phone.Shell;

namespace BYRForum
{
    public partial class MailListPage : PhoneApplicationPage
    {
        int current_page = 1;
        int total_page = 1;
        string box;
        ApplicationBarIconButton BarButtonFirstPage;
        ApplicationBarIconButton BarButtonPreviousPage;
        ApplicationBarIconButton BarButtonNextPage;
        ApplicationBarIconButton BarButtonLastPage;
        public MailListPage()
        {
            InitializeComponent();
            BarButtonFirstPage = this.ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            BarButtonPreviousPage = this.ApplicationBar.Buttons[1] as ApplicationBarIconButton;
            BarButtonNextPage = this.ApplicationBar.Buttons[2] as ApplicationBarIconButton;
            BarButtonLastPage = this.ApplicationBar.Buttons[3] as ApplicationBarIconButton;
            DisableApplicationBar();

        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            App.MailList.Items.Clear();
            MailListBox.ItemsSource = App.MailList.Items;
            this.PageTitle.Text = NavigationContext.QueryString["BoxName"];
            box=NavigationContext.QueryString["box"];
            String myRequestUrl = ByrApi.apiUrl + "/mail/"+box  + ByrApi.Encode + ByrApi.appkey+"page="+current_page;
            RequestState state = new RequestState();
            HttpUtil.HttpGetWithParam(state, myRequestUrl, HttpWebRequset_MailList_Callback);//发送请求
            progressBarWait.Visibility = Visibility.Visible;
        }
        private void HttpWebRequset_MailList_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                //网络有错误将会在这里抛出异常
                string result = HttpUtil.ReadToEndWithParam(asynchronousResult);
                DebugInfo.writeXml(result);
                string ErrorInfo;
                MailBox mail = ParseUtil.ParseXml<MailBox>(result, out ErrorInfo);
                current_page = Convert.ToInt32(mail.Pag.Page_current_count);
                total_page = Convert.ToInt32(mail.Pag.Page_all_count);
                this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    App.MailList.LoadData(mail.MailItems);
                    ShowApplicationBarState(current_page, total_page);
                    progressBarWait.Visibility = Visibility.Collapsed;
                    //App.ThreadsList.LoadData(mail.ArticleHeadItems);

                }));
            }
            catch
            {
                Debug.WriteLine("Error");
                this.Dispatcher.BeginInvoke(() =>
                {
                    progressBarWait.Visibility = Visibility.Collapsed;
                });
                //这里处理异常，可以在界面上抛出messagebox
            }
        }
        public void GotoPage(int num)
        {
            App.MailList.Items.Clear();
            String myRequestUrl = ByrApi.apiUrl + "/mail/"+box + ByrApi.Encode + ByrApi.appkey+"page="+num;
            RequestState state = new RequestState();
            HttpUtil.HttpGetWithParam(state, myRequestUrl, HttpWebRequset_MailList_Callback);//发送请求
            progressBarWait.Visibility = Visibility.Visible;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int nSelect=(sender as ListBox).SelectedIndex;
            if (nSelect == -1) return;
            this.NavigationService.Navigate(new Uri("/MailPage.xaml?BoxName=收件箱&index=" + App.MailList.Items[(sender as ListBox).SelectedIndex].Index.ToString()
                +"&box="+box, UriKind.Relative));
            nSelect = 1;
        }

        private void FirstPage_Click(object sender, EventArgs e)
        {
            GotoPage(1);
        }

        private void PreviousPage_Click(object sender, EventArgs e)
        {
            GotoPage(--current_page);
        }

        private void NextPage_Click(object sender, EventArgs e)
        {
            GotoPage(++current_page);
        }

        private void LastPage_Click(object sender, EventArgs e)
        {
            GotoPage(total_page);
        }
        public void ShowApplicationBarState(int current, int total)
        {
            BarButtonFirstPage.IsEnabled = true;
            BarButtonPreviousPage.IsEnabled = true;
            BarButtonNextPage.IsEnabled = true;
            BarButtonLastPage.IsEnabled = true;
            if (current == 1)
            {
                BarButtonFirstPage.IsEnabled = false;
                BarButtonPreviousPage.IsEnabled = false;
            }
            if (current == total)
            {
                BarButtonLastPage.IsEnabled = false;
                BarButtonNextPage.IsEnabled = false;
            }
        }

        protected void DisableApplicationBar()
        {
            BarButtonFirstPage.IsEnabled = false;
            BarButtonPreviousPage.IsEnabled = false;
            BarButtonNextPage.IsEnabled = false;
            BarButtonLastPage.IsEnabled = false;
        }
    }
}